{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-03-30T12:43:56.616702Z",
     "start_time": "2024-03-30T12:43:55.349925Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[0.1285],\n        [0.1034]], grad_fn=<AddmmBackward0>)"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "\n",
    "net = nn.Sequential(nn.Linear(4, 8), nn.ReLU(), nn.Linear(8, 1))\n",
    "X = torch.rand(size=(2, 4))\n",
    "net(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 5.2.1. 参数访问"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "9fce344672dbf0cb"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "OrderedDict([('weight',\n              tensor([[-0.2252, -0.0204,  0.0979,  0.0140,  0.1755,  0.2054,  0.3137, -0.0805]])),\n             ('bias', tensor([0.0724]))])"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net[2].state_dict()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T12:45:17.204805Z",
     "start_time": "2024-03-30T12:45:17.191203Z"
    }
   },
   "id": "5aea18851c385e48",
   "execution_count": 3
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 5.2.1.1. 目标参数"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "399bbcda87fc9f08"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(odict_keys(['a', 'b']),\n odict_values([1, 2]),\n odict_items([('a', 1), ('b', 2)]))"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import OrderedDict\n",
    "\n",
    "od = OrderedDict()\n",
    "od['a'] = 1\n",
    "od['b'] = 2\n",
    "od.keys(), od.values(), od.items()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T12:50:54.356005Z",
     "start_time": "2024-03-30T12:50:54.339911Z"
    }
   },
   "id": "79c9d5db9797357",
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'torch.nn.parameter.Parameter'>\n",
      "Parameter containing:\n",
      "tensor([0.0724], requires_grad=True)\n",
      "tensor([0.0724])\n"
     ]
    }
   ],
   "source": [
    "print(type(net[2].bias))\n",
    "print(net[2].bias)\n",
    "print(net[2].bias.data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T12:48:41.375341Z",
     "start_time": "2024-03-30T12:48:41.364004Z"
    }
   },
   "id": "c9da97caeeced98c",
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net[2].weight.grad == None"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T12:52:30.661787Z",
     "start_time": "2024-03-30T12:52:30.658769Z"
    }
   },
   "id": "a8c27e922510262b",
   "execution_count": 12
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 5.2.1.2. 一次性访问所有参数"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d8b5a78249897447"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('weight', torch.Size([8, 4])) ('bias', torch.Size([8]))\n",
      "('0.weight', torch.Size([8, 4])) ('0.bias', torch.Size([8])) ('2.weight', torch.Size([1, 8])) ('2.bias', torch.Size([1]))\n"
     ]
    }
   ],
   "source": [
    "print(*[(name, param.shape) for name, param in net[0].named_parameters()])\n",
    "print(*[(name, param.shape) for name, param in net.named_parameters()])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T12:53:34.368048Z",
     "start_time": "2024-03-30T12:53:34.358846Z"
    }
   },
   "id": "9ec2a673f573d45d",
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([0.0724])"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.state_dict()['2.bias'].data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T12:54:32.591352Z",
     "start_time": "2024-03-30T12:54:32.573018Z"
    }
   },
   "id": "b992b15102a2e306",
   "execution_count": 15
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 5.2.1.3. 从嵌套块收集参数"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "16cc2eceeb6d2de2"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[0.2602],\n        [0.2603]], grad_fn=<AddmmBackward0>)"
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def block1():\n",
    "    return nn.Sequential(nn.Linear(4, 8), nn.ReLU(), nn.Linear(8, 4), nn.ReLU())\n",
    "\n",
    "\n",
    "def block2():\n",
    "    net = nn.Sequential()\n",
    "    for i in range(4):\n",
    "        net.add_module(f'block{i}', block1())\n",
    "    return net\n",
    "\n",
    "\n",
    "rgnet = nn.Sequential(block2(), nn.Linear(4, 1))\n",
    "rgnet(X)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T13:33:09.808482Z",
     "start_time": "2024-03-30T13:33:09.790825Z"
    }
   },
   "id": "c49f739c82dbfe75",
   "execution_count": 41
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(Sequential(\n   (0): Sequential(\n     (block0): Sequential(\n       (0): Linear(in_features=4, out_features=8, bias=True)\n       (1): ReLU()\n       (2): Linear(in_features=8, out_features=4, bias=True)\n       (3): ReLU()\n     )\n     (block1): Sequential(\n       (0): Linear(in_features=4, out_features=8, bias=True)\n       (1): ReLU()\n       (2): Linear(in_features=8, out_features=4, bias=True)\n       (3): ReLU()\n     )\n     (block2): Sequential(\n       (0): Linear(in_features=4, out_features=8, bias=True)\n       (1): ReLU()\n       (2): Linear(in_features=8, out_features=4, bias=True)\n       (3): ReLU()\n     )\n     (block3): Sequential(\n       (0): Linear(in_features=4, out_features=8, bias=True)\n       (1): ReLU()\n       (2): Linear(in_features=8, out_features=4, bias=True)\n       (3): ReLU()\n     )\n   )\n   (1): Linear(in_features=4, out_features=1, bias=True)\n ),\n OrderedDict([('0.block0.0.weight',\n               tensor([[ 1.4298e-01,  2.0522e-01, -3.7490e-01,  1.6201e-02],\n                       [-2.2313e-01, -1.1530e-01,  4.1436e-01, -2.0983e-01],\n                       [ 4.4311e-02,  4.8856e-01, -2.1484e-01,  4.6087e-01],\n                       [-3.5535e-02, -1.9584e-01, -3.2442e-01,  2.2685e-01],\n                       [ 2.5914e-01,  7.4451e-02, -7.6627e-02, -5.9827e-02],\n                       [-2.4846e-01,  1.3956e-01,  4.4448e-01, -2.3805e-01],\n                       [-4.2874e-04,  2.6459e-01,  4.1078e-01,  9.3152e-02],\n                       [-2.2464e-01,  2.0948e-01, -1.4156e-01, -6.1895e-02]])),\n              ('0.block0.0.bias',\n               tensor([-0.2623,  0.2867, -0.2804,  0.1877, -0.0527, -0.1237,  0.1000, -0.2216])),\n              ('0.block0.2.weight',\n               tensor([[-0.0969, -0.1498, -0.0934,  0.1564, -0.3508,  0.0197, -0.2593,  0.2766],\n                       [-0.0776,  0.1528,  0.0132, -0.0761, -0.0561,  0.1683, -0.0084,  0.1516],\n                       [-0.1538, -0.1703,  0.2027, -0.2902, -0.2373,  0.2774, -0.1202,  0.1854],\n                       [-0.0793,  0.3525, -0.1089, -0.0629,  0.3361,  0.3479,  0.1501,  0.0800]])),\n              ('0.block0.2.bias',\n               tensor([-0.2110,  0.1299,  0.2805,  0.0816])),\n              ('0.block1.0.weight',\n               tensor([[-0.2636, -0.4431,  0.4786, -0.0026],\n                       [ 0.4379,  0.3928,  0.3246, -0.1645],\n                       [ 0.2489, -0.2738, -0.2587,  0.3492],\n                       [-0.4294, -0.2577,  0.1060, -0.4807],\n                       [-0.0806, -0.3205,  0.0635, -0.3053],\n                       [ 0.1493, -0.2747,  0.4999,  0.3195],\n                       [-0.1963,  0.2612,  0.0564, -0.2753],\n                       [-0.0803, -0.3711, -0.2876, -0.0597]])),\n              ('0.block1.0.bias',\n               tensor([ 0.2024, -0.3937,  0.2915,  0.3647, -0.3662,  0.4262,  0.4130,  0.4132])),\n              ('0.block1.2.weight',\n               tensor([[-0.1988,  0.1604,  0.0396, -0.1314, -0.0990,  0.2597, -0.2305,  0.3163],\n                       [ 0.0350, -0.1493, -0.0901,  0.0746, -0.2993, -0.1012, -0.0608, -0.0469],\n                       [-0.2861, -0.2117,  0.2272, -0.2982,  0.2626,  0.2718,  0.1763, -0.1641],\n                       [ 0.3316, -0.3503, -0.2221,  0.2094, -0.2685,  0.0936,  0.0226, -0.1253]])),\n              ('0.block1.2.bias',\n               tensor([ 0.2755, -0.3216,  0.2039,  0.1648])),\n              ('0.block2.0.weight',\n               tensor([[ 0.0940, -0.3250, -0.0224, -0.2570],\n                       [-0.2216,  0.3558,  0.2711,  0.1103],\n                       [ 0.2937, -0.2296,  0.3791,  0.0408],\n                       [-0.2124, -0.2130,  0.1239,  0.2880],\n                       [-0.2904, -0.3792,  0.2854,  0.2689],\n                       [-0.2715,  0.4790,  0.0545,  0.1605],\n                       [-0.0845,  0.0516,  0.3181, -0.0474],\n                       [-0.4367, -0.2000, -0.0827,  0.1159]])),\n              ('0.block2.0.bias',\n               tensor([ 0.4787,  0.4056, -0.4177, -0.3904,  0.4662,  0.3912,  0.2858,  0.0810])),\n              ('0.block2.2.weight',\n               tensor([[-0.3513,  0.1750, -0.2509,  0.2077,  0.3206, -0.1794, -0.1163,  0.0160],\n                       [ 0.0236,  0.3100,  0.1646,  0.1332, -0.1534,  0.0634, -0.3237,  0.3123],\n                       [ 0.1400,  0.3377,  0.0796,  0.3529,  0.0891,  0.1797, -0.3015, -0.1612],\n                       [-0.0930, -0.0453,  0.2426,  0.2011, -0.1330, -0.2963, -0.3428, -0.0525]])),\n              ('0.block2.2.bias',\n               tensor([-0.2929,  0.2034, -0.2183, -0.2735])),\n              ('0.block3.0.weight',\n               tensor([[-0.3108,  0.2455,  0.4929, -0.0875],\n                       [ 0.2527, -0.3651,  0.2053, -0.4034],\n                       [ 0.0208, -0.1921, -0.4784, -0.2343],\n                       [ 0.1718, -0.4677, -0.1139,  0.0883],\n                       [ 0.2870,  0.3062, -0.3867,  0.0351],\n                       [-0.0962,  0.2400,  0.3845, -0.3143],\n                       [-0.3742, -0.2011,  0.2226, -0.1902],\n                       [ 0.0485, -0.3470,  0.3924, -0.3929]])),\n              ('0.block3.0.bias',\n               tensor([-0.0080,  0.1245, -0.2146,  0.2822,  0.0363,  0.3097, -0.1547, -0.3105])),\n              ('0.block3.2.weight',\n               tensor([[-0.2121, -0.0514,  0.0868,  0.2076,  0.1131, -0.1400, -0.1112, -0.2885],\n                       [ 0.2186, -0.1204,  0.2757,  0.3172,  0.3008, -0.1768,  0.1662,  0.1745],\n                       [ 0.0119,  0.1961, -0.1851,  0.2697,  0.0548,  0.2258,  0.2212,  0.1744],\n                       [ 0.1417, -0.1841,  0.0551, -0.2091, -0.1228,  0.1405, -0.1054, -0.1172]])),\n              ('0.block3.2.bias',\n               tensor([-0.0083, -0.2837, -0.2772,  0.1185])),\n              ('1.weight', tensor([[-0.4703,  0.2782,  0.1283,  0.3960]])),\n              ('1.bias', tensor([0.2167]))]))"
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rgnet, rgnet.state_dict()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T13:33:32.853644Z",
     "start_time": "2024-03-30T13:33:32.842281Z"
    }
   },
   "id": "c95d7625c669b5b0",
   "execution_count": 43
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "Linear(in_features=4, out_features=8, bias=True)"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rgnet[0][1][0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T13:09:46.237087Z",
     "start_time": "2024-03-30T13:09:46.223912Z"
    }
   },
   "id": "d31cd3d47d2431dc",
   "execution_count": 20
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[ 0.0994, -0.3638, -0.3435,  0.2902],\n        [ 0.3275, -0.3670, -0.1597, -0.4588],\n        [ 0.1077, -0.3615,  0.1871,  0.4244],\n        [-0.0430,  0.3409,  0.3502,  0.0792],\n        [ 0.0273, -0.0147, -0.1602,  0.1731],\n        [-0.1249, -0.2324,  0.4107, -0.1609],\n        [-0.1609,  0.3367,  0.0032,  0.4671],\n        [-0.0962,  0.1268, -0.2380, -0.1700]])"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rgnet[0][1][0].weight.data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T13:11:01.197167Z",
     "start_time": "2024-03-30T13:11:01.187917Z"
    }
   },
   "id": "c38796d155fe52b5",
   "execution_count": 23
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 5.2.2. 参数初始化\n",
    "\n",
    "#### 5.2.2.1. 内置初始化"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1c17875bd641667c"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([[-0.0048,  0.0003,  0.0317,  0.0029],\n         [ 0.0046, -0.0053,  0.0117,  0.0111],\n         [-0.0150, -0.0051, -0.0144, -0.0049],\n         [ 0.0198, -0.0088,  0.0079, -0.0101],\n         [-0.0064,  0.0091, -0.0002,  0.0008],\n         [ 0.0108,  0.0139, -0.0038, -0.0102],\n         [-0.0079, -0.0218, -0.0031, -0.0098],\n         [-0.0109,  0.0235,  0.0078, -0.0026]]),\n tensor([0., 0., 0., 0., 0., 0., 0., 0.]))"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def init_normal(m):\n",
    "    if type(m) == nn.Linear:\n",
    "        nn.init.normal_(m.weight, mean=0, std=0.01)\n",
    "        nn.init.zeros_(m.bias)\n",
    "\n",
    "\n",
    "net.apply(init_normal)\n",
    "net[0].weight.data, net[0].bias.data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T13:13:33.848320Z",
     "start_time": "2024-03-30T13:13:33.827909Z"
    }
   },
   "id": "e5400a1d26f32e67",
   "execution_count": 25
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([[1., 1., 1., 1.],\n         [1., 1., 1., 1.],\n         [1., 1., 1., 1.],\n         [1., 1., 1., 1.],\n         [1., 1., 1., 1.],\n         [1., 1., 1., 1.],\n         [1., 1., 1., 1.],\n         [1., 1., 1., 1.]]),\n tensor([0., 0., 0., 0., 0., 0., 0., 0.]))"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def init_constant(m):\n",
    "    if type(m) == nn.Linear:\n",
    "        nn.init.constant_(m.weight, 1)\n",
    "        nn.init.zeros_(m.bias)\n",
    "\n",
    "\n",
    "net.apply(init_constant)\n",
    "net[0].weight.data, net[0].bias.data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T13:14:16.357157Z",
     "start_time": "2024-03-30T13:14:16.306700Z"
    }
   },
   "id": "8683239fc7fd4736",
   "execution_count": 26
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.6460, -0.6775, -0.3585,  0.6613],\n",
      "        [-0.5584,  0.1139, -0.6693, -0.4241],\n",
      "        [ 0.5262, -0.6114, -0.6416,  0.6465],\n",
      "        [ 0.6119, -0.5928, -0.4257,  0.4076],\n",
      "        [-0.4936, -0.0362, -0.1997,  0.1496],\n",
      "        [-0.1318,  0.0246, -0.1898, -0.5782],\n",
      "        [ 0.2222, -0.5028, -0.1161, -0.6711],\n",
      "        [-0.4280, -0.6381,  0.2611,  0.5366]])\n",
      "tensor([[42., 42., 42., 42., 42., 42., 42., 42.]])\n"
     ]
    }
   ],
   "source": [
    "def init_xavier(m):\n",
    "    if type(m) == nn.Linear:\n",
    "        nn.init.xavier_uniform_(m.weight)\n",
    "\n",
    "\n",
    "def init_42(m):\n",
    "    if type(m) == nn.Linear:\n",
    "        nn.init.constant_(m.weight, 42)\n",
    "\n",
    "\n",
    "net[0].apply(init_xavier)\n",
    "net[2].apply(init_42)\n",
    "print(net[0].weight.data)\n",
    "print(net[2].weight.data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T13:14:47.979828Z",
     "start_time": "2024-03-30T13:14:47.972319Z"
    }
   },
   "id": "32ea34e25d1bade0",
   "execution_count": 27
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 5.2.2.2. 自定义初始化"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4121cbd67125a10f"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Init weight torch.Size([8, 4])\n",
      "tensor([[False,  True,  True,  True],\n",
      "        [False,  True,  True, False],\n",
      "        [False,  True,  True, False],\n",
      "        [False,  True,  True, False],\n",
      "        [False,  True,  True, False],\n",
      "        [False, False,  True,  True],\n",
      "        [False,  True,  True, False],\n",
      "        [False,  True, False, False]])\n",
      "tensor([[0., 1., 1., 1.],\n",
      "        [0., 1., 1., 0.],\n",
      "        [0., 1., 1., 0.],\n",
      "        [0., 1., 1., 0.],\n",
      "        [0., 1., 1., 0.],\n",
      "        [0., 0., 1., 1.],\n",
      "        [0., 1., 1., 0.],\n",
      "        [0., 1., 0., 0.]])\n",
      "Init weight torch.Size([1, 8])\n",
      "tensor([[False, False, False,  True,  True, False, False, False]])\n",
      "tensor([[0., 0., 0., 1., 1., 0., 0., 0.]])\n"
     ]
    },
    {
     "data": {
      "text/plain": "Parameter containing:\ntensor([[ 0.0000,  8.0558,  5.4181,  5.4241],\n        [ 0.0000, -6.9734, -6.9802,  0.0000],\n        [ 0.0000,  7.2593, -5.8514, -0.0000],\n        [ 0.0000, -9.2029,  9.9661, -0.0000],\n        [-0.0000,  6.2810,  8.3928, -0.0000],\n        [-0.0000, -0.0000,  7.8942, -9.5955],\n        [ 0.0000,  7.1894, -8.1557,  0.0000],\n        [ 0.0000,  9.4360, -0.0000, -0.0000]], requires_grad=True)"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def my_init(m):\n",
    "    if type(m) == nn.Linear:\n",
    "        print(\"Init\", *[(name, param.shape) for name, param in m.named_parameters()][0])\n",
    "        nn.init.uniform_(m.weight, -10, 10)\n",
    "        temp = torch.ones(m.weight.shape)\n",
    "        print(m.weight.data.abs() >= 5)\n",
    "        temp *= m.weight.data.abs() >= 5\n",
    "        print(temp)\n",
    "        m.weight.data *= m.weight.data.abs() >= 5\n",
    "\n",
    "\n",
    "net.apply(my_init)\n",
    "net[0].weight"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T13:27:54.736763Z",
     "start_time": "2024-03-30T13:27:54.727652Z"
    }
   },
   "id": "a34e4b443cc05ffd",
   "execution_count": 34
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([42.0000,  9.0558,  6.4181,  6.4241])"
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net[0].weight.data[:] += 1\n",
    "net[0].weight.data[0, 0] = 42\n",
    "net[0].weight.data[0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T13:29:21.292551Z",
     "start_time": "2024-03-30T13:29:21.229944Z"
    }
   },
   "id": "50a0ac3b16f9d897",
   "execution_count": 35
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 5.2.3. 参数绑定\n",
    "\n",
    "共享层梯度在反向传播时会叠加"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "11a7375871da4efc"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([True, True, True, True, True, True, True, True])\n",
      "tensor([True, True, True, True, True, True, True, True])\n"
     ]
    }
   ],
   "source": [
    "# 我们需要给共享层一个名称，以便可以引用它的参数\n",
    "shared = nn.Linear(8, 8)\n",
    "net = nn.Sequential(nn.Linear(4, 8), nn.ReLU(),\n",
    "                    shared, nn.ReLU(),\n",
    "                    shared, nn.ReLU(),\n",
    "                    nn.Linear(8, 1))\n",
    "net(X)\n",
    "# 检查参数是否相同\n",
    "print(net[2].weight.data[0] == net[4].weight.data[0])\n",
    "net[2].weight.data[0, 0] = 100\n",
    "# 确保它们实际上是同一个对象，而不只是有相同的值\n",
    "print(net[2].weight.data[0] == net[4].weight.data[0])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T13:30:39.299414Z",
     "start_time": "2024-03-30T13:30:39.279099Z"
    }
   },
   "id": "934971eca80100e6",
   "execution_count": 36
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "Sequential(\n  (0): Linear(in_features=4, out_features=8, bias=True)\n  (1): ReLU()\n  (2): Linear(in_features=8, out_features=8, bias=True)\n  (3): ReLU()\n  (4): Linear(in_features=8, out_features=8, bias=True)\n  (5): ReLU()\n  (6): Linear(in_features=8, out_features=1, bias=True)\n)"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T13:30:59.741925Z",
     "start_time": "2024-03-30T13:30:59.729334Z"
    }
   },
   "id": "a0b99ea49146889b",
   "execution_count": 37
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "OrderedDict([('0.weight',\n              tensor([[ 0.1269, -0.3846, -0.0375,  0.0739],\n                      [-0.3093, -0.0683, -0.2190, -0.0597],\n                      [ 0.4926,  0.2397,  0.4199,  0.4060],\n                      [ 0.3214, -0.0743,  0.4622,  0.1462],\n                      [ 0.2990,  0.0577, -0.3573, -0.3652],\n                      [-0.3391, -0.1128, -0.0414, -0.4495],\n                      [ 0.2293,  0.3628,  0.1732,  0.3468],\n                      [-0.0120,  0.2138, -0.4135,  0.4842]])),\n             ('0.bias',\n              tensor([0.1767, 0.0564, 0.1537, 0.0781, 0.0323, 0.4173, 0.2836, 0.1310])),\n             ('2.weight',\n              tensor([[ 1.0000e+02,  2.7174e-01,  3.0019e-01,  2.1212e-01, -3.3386e-01,\n                        2.7058e-01, -3.1236e-02,  8.7230e-02],\n                      [ 1.1070e-02, -1.0690e-01,  1.3594e-02, -9.3688e-02,  1.1146e-02,\n                        3.0032e-02, -5.5711e-02, -2.0671e-01],\n                      [ 3.3081e-01,  1.6884e-01,  1.6399e-01,  6.6523e-02, -2.3124e-02,\n                        2.3643e-02, -2.1868e-01,  2.0450e-01],\n                      [-2.7873e-01, -2.1915e-01, -3.2660e-02,  1.9287e-01,  1.7847e-02,\n                       -7.8018e-02,  1.9601e-01,  2.9212e-01],\n                      [-1.7522e-01, -1.4647e-01, -2.3921e-02,  3.0155e-01, -4.7848e-02,\n                        2.5987e-01, -2.2190e-01, -2.9403e-01],\n                      [ 3.2638e-01, -2.9785e-01,  2.5093e-01, -2.4048e-01,  7.7701e-04,\n                        4.1047e-02, -3.8562e-02,  1.0523e-02],\n                      [-3.2819e-01,  6.5235e-02,  3.0011e-01, -5.6478e-02,  2.7336e-01,\n                        3.3945e-01, -1.5288e-01,  9.5671e-02],\n                      [ 1.3150e-01, -1.6303e-01,  2.7603e-01, -1.2598e-01, -1.4096e-01,\n                       -1.5820e-01, -1.2893e-01, -2.3185e-01]])),\n             ('2.bias',\n              tensor([-0.2934, -0.3144, -0.3277,  0.2547,  0.3304,  0.1966, -0.1299, -0.2456])),\n             ('4.weight',\n              tensor([[ 1.0000e+02,  2.7174e-01,  3.0019e-01,  2.1212e-01, -3.3386e-01,\n                        2.7058e-01, -3.1236e-02,  8.7230e-02],\n                      [ 1.1070e-02, -1.0690e-01,  1.3594e-02, -9.3688e-02,  1.1146e-02,\n                        3.0032e-02, -5.5711e-02, -2.0671e-01],\n                      [ 3.3081e-01,  1.6884e-01,  1.6399e-01,  6.6523e-02, -2.3124e-02,\n                        2.3643e-02, -2.1868e-01,  2.0450e-01],\n                      [-2.7873e-01, -2.1915e-01, -3.2660e-02,  1.9287e-01,  1.7847e-02,\n                       -7.8018e-02,  1.9601e-01,  2.9212e-01],\n                      [-1.7522e-01, -1.4647e-01, -2.3921e-02,  3.0155e-01, -4.7848e-02,\n                        2.5987e-01, -2.2190e-01, -2.9403e-01],\n                      [ 3.2638e-01, -2.9785e-01,  2.5093e-01, -2.4048e-01,  7.7701e-04,\n                        4.1047e-02, -3.8562e-02,  1.0523e-02],\n                      [-3.2819e-01,  6.5235e-02,  3.0011e-01, -5.6478e-02,  2.7336e-01,\n                        3.3945e-01, -1.5288e-01,  9.5671e-02],\n                      [ 1.3150e-01, -1.6303e-01,  2.7603e-01, -1.2598e-01, -1.4096e-01,\n                       -1.5820e-01, -1.2893e-01, -2.3185e-01]])),\n             ('4.bias',\n              tensor([-0.2934, -0.3144, -0.3277,  0.2547,  0.3304,  0.1966, -0.1299, -0.2456])),\n             ('6.weight',\n              tensor([[ 0.1989, -0.3108, -0.1332,  0.2665, -0.0515,  0.2413, -0.1960, -0.3332]])),\n             ('6.bias', tensor([-0.1797]))])"
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.state_dict()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T13:31:21.182686Z",
     "start_time": "2024-03-30T13:31:21.174624Z"
    }
   },
   "id": "92f5b01399b91e4e",
   "execution_count": 38
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.weight -> Parameter containing:\n",
      "tensor([[ 0.1269, -0.3846, -0.0375,  0.0739],\n",
      "        [-0.3093, -0.0683, -0.2190, -0.0597],\n",
      "        [ 0.4926,  0.2397,  0.4199,  0.4060],\n",
      "        [ 0.3214, -0.0743,  0.4622,  0.1462],\n",
      "        [ 0.2990,  0.0577, -0.3573, -0.3652],\n",
      "        [-0.3391, -0.1128, -0.0414, -0.4495],\n",
      "        [ 0.2293,  0.3628,  0.1732,  0.3468],\n",
      "        [-0.0120,  0.2138, -0.4135,  0.4842]], requires_grad=True)\n",
      "0.bias -> Parameter containing:\n",
      "tensor([0.1767, 0.0564, 0.1537, 0.0781, 0.0323, 0.4173, 0.2836, 0.1310],\n",
      "       requires_grad=True)\n",
      "2.weight -> Parameter containing:\n",
      "tensor([[ 1.0000e+02,  2.7174e-01,  3.0019e-01,  2.1212e-01, -3.3386e-01,\n",
      "          2.7058e-01, -3.1236e-02,  8.7230e-02],\n",
      "        [ 1.1070e-02, -1.0690e-01,  1.3594e-02, -9.3688e-02,  1.1146e-02,\n",
      "          3.0032e-02, -5.5711e-02, -2.0671e-01],\n",
      "        [ 3.3081e-01,  1.6884e-01,  1.6399e-01,  6.6523e-02, -2.3124e-02,\n",
      "          2.3643e-02, -2.1868e-01,  2.0450e-01],\n",
      "        [-2.7873e-01, -2.1915e-01, -3.2660e-02,  1.9287e-01,  1.7847e-02,\n",
      "         -7.8018e-02,  1.9601e-01,  2.9212e-01],\n",
      "        [-1.7522e-01, -1.4647e-01, -2.3921e-02,  3.0155e-01, -4.7848e-02,\n",
      "          2.5987e-01, -2.2190e-01, -2.9403e-01],\n",
      "        [ 3.2638e-01, -2.9785e-01,  2.5093e-01, -2.4048e-01,  7.7701e-04,\n",
      "          4.1047e-02, -3.8562e-02,  1.0523e-02],\n",
      "        [-3.2819e-01,  6.5235e-02,  3.0011e-01, -5.6478e-02,  2.7336e-01,\n",
      "          3.3945e-01, -1.5288e-01,  9.5671e-02],\n",
      "        [ 1.3150e-01, -1.6303e-01,  2.7603e-01, -1.2598e-01, -1.4096e-01,\n",
      "         -1.5820e-01, -1.2893e-01, -2.3185e-01]], requires_grad=True)\n",
      "2.bias -> Parameter containing:\n",
      "tensor([-0.2934, -0.3144, -0.3277,  0.2547,  0.3304,  0.1966, -0.1299, -0.2456],\n",
      "       requires_grad=True)\n",
      "6.weight -> Parameter containing:\n",
      "tensor([[ 0.1989, -0.3108, -0.1332,  0.2665, -0.0515,  0.2413, -0.1960, -0.3332]],\n",
      "       requires_grad=True)\n",
      "6.bias -> Parameter containing:\n",
      "tensor([-0.1797], requires_grad=True)\n"
     ]
    }
   ],
   "source": [
    "for name, value in net.named_parameters():\n",
    "    print('{} -> {}'.format(name, value))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T13:32:09.599924Z",
     "start_time": "2024-03-30T13:32:09.582794Z"
    }
   },
   "id": "df5859a05e570747",
   "execution_count": 40
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "c7def064280b3f3d"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
